<?php

/**
 * Atakan KOC Framework
 * -----------------------------------------------
 * Copyright (C) 2009 Atakan KOC
 * PHP versions 4 and 5
 * -----------------------------------------------
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * http://www.gnu.org/copyleft/gpl.html
 */
 
class Site_Init
{
  var $db;
  var $timer;
  var $data = array();
  var $data_r = array();
  var $data_g = array();
  var $data_p = array();
  var $data_c = array();
  var $data_s = array();
  var $data_e = array();
  var $data_f = array();
  var $template_cache = array();
  var $timer_start = 0;
  var $timer_end = 0;
  var $timer_total_time = 0;
  var $memory_start = 0;
  var $memory_end = 0;
  var $timer_text = '';
  var $memory_text = '';
    
  function Site_Init() 
  {
    global $config,$lang;
    $this->timer();

    foreach ($_GET as $check_url) {
      if ((eregi("<[^>]*script*\"?[^>]*>", $check_url)) || (eregi("<[^>]*object*\"?[^>]*>", $check_url)) ||
        (eregi("<[^>]*iframe*\"?[^>]*>", $check_url)) || (eregi("<[^>]*applet*\"?[^>]*>", $check_url)) ||
        (eregi("<[^>]*meta*\"?[^>]*>", $check_url)) || (eregi("<[^>]*style*\"?[^>]*>", $check_url)) ||
        (eregi("<[^>]*form*\"?[^>]*>", $check_url)) || (eregi("\([^>]*\"?[^)]*\)", $check_url)) ||
        (eregi("\"", $check_url))) 
      {
        die ();
      }
    }
    unset($check_url);

    $this->data_r = $this->strip_slashes_array($_REQUEST);
    $this->data_g = $this->strip_slashes_array($_GET);
    $this->data_p = $this->strip_slashes_array($_POST);
    $this->data_c = $this->strip_slashes_array($_COOKIE);
    $this->data_s = $this->strip_slashes_array($_SERVER);
    $this->data_e = $this->strip_slashes_array($_ENV);
    $this->data_f = $this->strip_slashes_array($_FILES);
    $this->data = array_merge($this->data_r, $this->data_g, $this->data_p,
    $this->data_c, $this->data_s, $this->data_e, $this->data_f);
  }

  function strip_slashes_array($array)
  {
    $return_array = array();
	
    foreach($array as $key => $val)
    {
      if(is_array($val))
      {
        $return_array[$key] = $this->strip_slashes_array($val);
      }
      else
      {
        $return_array[$key] = stripslashes($val);
      }
    }
	
	 return $return_array;
  }

	function xss_clean($var)
	{
		static
			$preg_find    = array('#^javascript#i', '#^vbscript#i'),
			$preg_replace = array('java script',   'vb script');

		return preg_replace($preg_find, $preg_replace, htmlspecialchars(trim($var)));
	}
    
  function get_template($templatename)
  {
    global $config,$lang;  
    $get_temp = DIR.'/styles/'.$config['templatename'].'/'.$templatename.'.html';
    if ($this->template_cache["$templatename"])
    {
    return $this->template_cache["$templatename"];
    }
    if (file_exists($get_temp))
    {
      $get_temp = file_get_contents($get_temp);
      $result = "return <<<TMP\r\n{$get_temp}\r\nTMP;\r\n";
      $this->template_cache["$templatename"] = $result;
      return $result;
    }
    else
    {
    die($lang['template_not_found'].' : '.$get_temp);
    }
  }
  
  function get_stats()
  {
    $this->stop();
    
    $result = $this->timer_text . ' ' . $this->memory_text;
    return $result;
  }

  function timer()
  {
    if(!$this->timer_start) 
    {
      $temp_time = explode(" ", microtime());
      $this->timer_start = $temp_time[1] + $temp_time[0];
    }
    
		if (function_exists('memory_get_usage'))
		{
			$this->memory_start = memory_get_usage();
		}
		
  }

	function stop()
	{
		if($this->timer_start)
		{
			$temp_time = explode(" ", microtime());
			$this->timer_end = $temp_time[1] + $temp_time[0];
			$this->timer_total_time = $this->timer_end - $this->timer_start;
			$this->timer_text = number_format($this->timer_total_time, 4);
		}
		
		if (function_exists('memory_get_usage'))
		{
			$this->memory_end = memory_get_usage();
		}
		
		if($this->memory_start)
		{
      $this->memory_text = number_format(($this->memory_end - $this->memory_start) / 1024, 3). " KB";
		}
	}
	
  
}
?>
